<?php

namespace App\V3\V3_2_7\Services;

use App\V3\V3_2_7\Models\SupplierCustomerModel;
use Illuminate\Support\Facades\DB;

class SupplierCustomerService
{
    //检查客户信息
    static public function checkInfo($data)
    {
        $result = SupplierCustomerModel::checkInfo($data);

        if(empty($result)) {
            return true;
        }
        else {
            return false;
        }
    }

    //列表
    static public function index($data)
    {
        $keyword = isset($data['keyword'])?$data['keyword']:'';
        if($data['type'] == 1){
            return SupplierCustomerModel::customerIndex(//客户列表
                $keyword, $data
                );
        }else{
            return SupplierCustomerModel::index(//供应商
                $keyword, $data
            );
        }
    }

    //编辑
    static public function edit($data)
    {
        //搜索要修改的目标数据
/*        $searchTargetInfo = SupplierCustomerModel::where([
            'id' => $data['id'],
            'user_id' => $data['user_id']
        ])->first();

        $searchSameInfo = SupplierCustomerModel::where([
            'compony_name' => empty($searchTargetInfo['compony_name'])?'':$searchTargetInfo['compony_name'],
            'name' => empty($searchTargetInfo['name'])?'':$searchTargetInfo['name'],
            'phone' => empty($searchTargetInfo['phone'])?'':$searchTargetInfo['phone'],
            'address' => empty($searchTargetInfo['address'])?'':$searchTargetInfo['address'],
            'user_id' => $data['user_id']
        ])->get()->toArray();

        //批量更新
        foreach($searchSameInfo as $value) {
            SupplierCustomerModel::where('id', $value['id'])
                ->update([
                    'compony_name' => empty($data['compony_name'])?'':$data['compony_name'],
                    'name' => empty($data['name'])?'':$data['name'],
                    'phone' => empty($data['phone'])?'':$data['phone'],
                    'address' => empty($data['address'])?'':$data['address'],
                    'home_phone' => empty($data['home_phone'])?'':$data['home_phone'],
                    'remark' => empty($data['remark'])?'':$data['remark'],
                    'address' => empty($data['address'])?'':$data['address'],
                ]);
        }*/

        SupplierCustomerModel::where('id', $data['id'])->update($data);

        return true;
    }

    // $type 单据类型1:采购单;0:销售单;主账号获取列表
    static public function mainGetList($type, $mainId)
    {
        $list=SupplierCustomerModel::where([
            'type' => $type,
            'delete_flag' => 0,
            'main_id' => $mainId
        ])
        ->orderBy('id', 'desc')
        ->get()->toArray();
        foreach ($list as &$v){

            if(!empty($v['phone'])){

                $v['phone']=explode(',', $v['phone']);
            }
        }
        return $list;
    }

    /**
     * 子账号获取客户列表
     * @param $type
     * @param $isShareCustomer 是否共享主体下所有客户;0:否;1:是;
     * @param $userId
     * @param $mainId
     * AUTHOR  : Sun Wu
     * DateTime: 2022/8/5 17:58
     * @return mixed
     */
    static public function getCustomerList($type, $isShareCustomer, $userId, $mainId,$keyword)
    {
        $condition = [];

        if($userId == $mainId) {

            $condition['main_id'] = $mainId;
        }
        else {
            if($isShareCustomer) {

                $condition['main_id'] = $mainId;

            }
            else {
                $condition['user_id'] = $userId;
            }
        }

       $list= SupplierCustomerModel::where([

            'delete_flag' => 0,
        ])
        ->where($condition)->where('type',1);

        $list = $list->where(function($query) use ($keyword) {
            $query->orWhere('name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('compony_name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('phone', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('address', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('home_phone', 'LIKE', '%'.$keyword.'%');
        })
        ->orderBy('id', 'desc')
        ->get()->toArray();

        foreach ($list as &$v){

            if(!empty($v['phone'])){

                $v['phone']=explode(',', $v['phone']);
            }
        }
        return $list;
    }

    /**
     * 子账号获取供应商客户列表
     * @param $type
     * @param $isSetAsCustomer
     * @param $userId
     * AUTHOR  : Sun Wu
     * DateTime: 2022/8/5 18:04
     * @return mixed
     */
    static public function getSupplierList($type, $isShareCustomer,$isShareSupplier,$isSetAsSupplier, $userId, $mainId,$keyword)
    {
        $query = SupplierCustomerModel::where([
            'delete_flag' => 0
        ])->where(function($query) use ($keyword) {
            $query->orWhere('name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('compony_name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('phone', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('address', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('home_phone', 'LIKE', '%'.$keyword.'%');
        });

             if($isShareCustomer) {//开启共享客户
                 if($isSetAsSupplier){//设置客户为供应商
                     $query->where('main_id', $mainId);
                     /* $query->whereIn('type', [0,1]); */

                 }else{//未设置客户为供应商
                     if($isShareSupplier){
                         $query->where('main_id', $mainId);
                         $query->where('type', 0);
                     }else{
                         $query->where('main_id', $mainId);
                         $query->where('user_id', $userId);
                         $query->where('type', 0);
                     }
                 }

             }elseif(!$isShareCustomer && $isShareSupplier) {//开启共享客户
                 if($isSetAsSupplier){//设置客户为供应商
                     $query->where('main_id', $mainId);
                 }else{//未设置客户为供应商
                     $query->where('main_id', $mainId);
                     $query->where('type', 0);
                 }

             }else{//未开启共享客户

                if($mainId == $userId && !$isSetAsSupplier) {//主账户等于操作人，未设置客户做为供应商

                    $query->where('main_id', $mainId)->where('type', 0);

                }
                elseif($mainId == $userId && $isSetAsSupplier) {//主账户等于操作人，设置客户做为供应商

                    $query->where('main_id', $mainId);

                }elseif($mainId != $userId && !$isSetAsSupplier){//主账户不等于操作人，未设置客户做为供应商
                    $query->where('main_id', $mainId);
                    $query->where('user_id', $userId);
                    $query->where('type', 0);
                }elseif($mainId != $userId && $isSetAsSupplier){//主账户不等于操作人，设置客户做为供应商
                    $query->where('main_id', $mainId);
                    $query->where('user_id', $userId);
                    /* $query->whereIn('type', 0); */
                }
             }
       /*  if(!$isShareCustomer) {
            $query->where('main_id', $mainId);
            $query->where('user_id', $userId);
            $query->whereIn('type', 0);
        }else{
            $query->where('main_id', $mainId);
            $query->where('type', 0);
        } */

        $list = $query->orderBy('id', 'desc')
        ->get()->toArray();

        foreach ($list as &$v){

            if(!empty($v['phone'])){

                $v['phone']=explode(',', $v['phone']);
            }
        }
        return $list;

    }
}
